
package guoan.web.webCustomer;

import guoan.core.dto.DataGridModel;
import guoan.core.dto.PageResponse;
import guoan.core.dto.RestResponse;
import guoan.domain.Adv;
import guoan.domain.AdvType;
import guoan.service.AdvService;
import guoan.service.AdvTypeService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.base.Preconditions;

@Controller
@RequestMapping("/webCustomer/adv")
public class AdvController {
    protected final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private AdvService advService;

    @Autowired
    private AdvTypeService advTypeService;

    /**
     * 跳转到不同的内容页面
     * 
     * @param type
     * @return
     */
    @RequestMapping(value = "index_{type}", method = RequestMethod.GET)
    public String index(@PathVariable("type") String type){
        return "/webCustomer/adv/" + type + "/index";
    }

    @RequestMapping(value = "index_{type}/data", method = RequestMethod.POST)
    @ResponseBody
    public PageResponse<Adv> data(@PathVariable("type") String type,
            DataGridModel dataGridModel){
        return advService.page(type, dataGridModel);
    }

    /** 查看 */
    @RequestMapping(value = { "index_{type}/update", "index_{type}/add" },
            method = RequestMethod.GET)
    public String toUpdate(Long id, @PathVariable("type") String type,
            Model model){
        if(type.equals("home")){
            Adv adv = advService.findHome();
            model.addAttribute("adv", adv);
        }
        if (id != null) {
            Adv adv = advService.findOne(id);
            model.addAttribute("adv", adv);
        }

        List<AdvType> advTypes = advTypeService.find();
        model.addAttribute("advTypes", advTypes);

        return "/webCustomer/adv/" + type + "/add";
    }

    /** 保存 */
    @RequestMapping(value = "index_{type}/save", method = RequestMethod.POST)
    @ResponseBody
    public RestResponse<Boolean> save(Adv adv, @RequestParam(
            value = "advTypeId", required = false) Long advTypeId,
            @RequestParam(value = "keyName", required = false) String keyName,
            @PathVariable("type") String type, Model model,
            HttpServletRequest request){
        Preconditions.checkNotNull(adv, "参数错误");
        adv.setType(type);
        advService.save(adv, advTypeId, keyName, request.getSession()
                .getServletContext().getRealPath("/"));
        return RestResponse.ok(true);
    }

    @RequestMapping(value = "index_{type}/del", method = RequestMethod.GET)
    @ResponseBody
    public RestResponse<Boolean> del(@PathVariable("type") String type, Long id){
        Preconditions.checkNotNull(id, "参数错误");
        advService.del(id);
        return RestResponse.ok(true);
    }

}
